<?php
namespace Admin\Controller;
use Common\Controller\BaseController;

class CarController extends BaseController 
{
    public $page_size = 10;
    
    public function _initialize()
    {
        parent::_initialize();
        self::$_action_block = 1;
        $this->assign('block',self::$_action_block);
    }
    public function index()
    {
        $type = I('get.type',0,'intval');
        $carModel = D('Car'); //实例化car
        $where = "  car.is_drift=1";
        // $display_page;
        switch ($type)    //判断在header的链接里传过来type值是多少，1待审核，2已通过，3未通过
        {
            case '1':
                $where .=" and car.status=1 ";
                break;
            case '2':
                $where .=" and car.status=2 ";
                break;
            case '3':
                $where .=" and car.status=3 ";
                break;
        }

        $count = $carModel
                ->join('staff on staff.staff_id=car.staff_id','LEFT')
                ->where($where)
                ->count();    //判断记录数量多少

        vendor('Page');
        $Page  = new \Vendor\Page($count,$this->page_size);

        $field = "car.*,staff.staff_name ";
        $list = $carModel->field($field)
                ->order('apply_time DESC')
                ->join('staff on staff.staff_id=car.staff_id','LEFT')
                ->where($where)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        $this->assign('type',$type);
        $this->assign('list',$list);
        $this->assign('page',$Page->show());
        $this->display('index');
    }

    public function view()   //index.html 中的链接引发的view方法
    {
        $id     = I('get.id',0,'intval');   //在index.html的链接中传过来id和type值，id和type在index方法中的list变量中存在
        $type   = I('get.type',0,'intval');  
        
        $carModel  = D('Car');
        $field = " car.*,staff.*";
        $info = $carModel
                ->field($field)
                ->join('staff on car.staff_id=staff.staff_id')
                ->where("car.id={$id}")
                ->select();

        if(empty($info))
        {
            $this->error('数据不存在');
        }
        $this->assign('info',$info[0]);
        $view   = 'view_'.$type; 
        $this->display($view);    
    }

    public function judge()
    {
        $id       = I('post.car_id',0,'intval');
        $status   = I('post.status',1,'intval'); 
        $remark   = I('post.remark','','htmlspecialchars');  
        $carModel  = D('Car');
        $res = $carModel->where("id={$id}")->select();
    
        if(empty($res))
        {
            $this->error('数据不存在');
        }

        $data = array(
        'id'    =>$id,
        'status'=>$status,
        'remark'=>$remark
        );

        $res = $carModel->save($data);
        if(false !== $res)
        {
            $this->success('操作成功',U('Admin/Car/index'));
        }
        else
        {
            $this->error('操作失败');
        }
    }
  
}